<template>
  <div class="app-container">
    <router-link to="brand/create">
      <el-button type="primary" size="small"><i class="el-icon-plus" /> 新增品牌</el-button>
    </router-link>

    <el-card class="table_style">
      <el-table v-loading="listLoading" :data="brands" element-loading-text="Loading" border>
        <el-table-column label="ID" prop="id" width="80" />
        <el-table-column prop="name" label="品牌名称" />
        <el-table-column prop="logo" label="logo图">
          <template slot-scope="scope">
            <img v-if="scope.row.logo" :src="scope.row.logo" :alt="scope.row.name" class="pic-style">
            <el-tag v-else type="warning">暂无logo</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="image" label="缩略图">
          <template slot-scope="scope">
            <img v-if="scope.row.image" :src="scope.row.image" :alt="scope.row.name" class="pic-style">
            <el-tag v-else type="warning">暂无缩略图</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="thumb" label="展示图">
          <template slot-scope="scope">
            <img v-if="scope.row.thumb" :src="scope.row.thumb" :alt="scope.row.name" class="pic-style">
            <el-tag v-else type="warning">暂无展示图</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="enable" label="是否启用">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enable" @change="changeAttr(scope.row,'enable')" />
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="描述信息" placement="top" enterable>
              <el-button
                size="mini"
                type="success"
                icon="el-icon-star-off"
                @click="dialogShow(scope.row)"
              />
            </el-tooltip>
            <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--描述信息-->
    <el-dialog title="描述信息" :visible.sync="dialogTableVisible">
      <el-form :model="brand">
        <el-form-item label="详细内容" :label-width="formLabelWidth">
          <el-input v-model="brand.introduce" type="textarea" :rows="10" disabled />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { fetchBrandList, deleteBrand, ChangeEnableBrand, fetchBrand } from '@/api/brand'

export default {
  data() {
    return {
      brands: [],
      listLoading: true,
      brand: {},
      dialogTableVisible: false,
      formLabelWidth: '120px'
    }
  },
  created() {
    this.init()
  },
  methods: {
    async init() {
      this.listLoading = true
      const res = await fetchBrandList()
      this.brands = res.data.brands
      this.listLoading = false
    },
    // 显示品牌的描述信息
    dialogShow(row) {
      this.dialogTableVisible = true
      fetchBrand(row.id).then((res) => {
        this.brand = res.data.brand
      })
    },
    // 点击跳转到编辑页面
    handleEdit(row) {
      this.$router.push({
        name: 'BrandEdit',
        params: { id: row.id }
      })
    },
    // 是否启用
    async changeAttr(row, enable) {
      const res = await ChangeEnableBrand(row.id, { enable: enable })
      if (res.code === 20000) return this.$message.success(res.message)
      return this.$message.error(res.message)
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确认要删除该品牌吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(async() => {
        const res = await deleteBrand(row.id)
        this.init()
        this.$message({
          type: 'success',
          message: res.message
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped>
.table_style {
  margin-top: 15px;
}
.pic-style{
  width: 80px;
  height: 60px;
}
</style>
